package com.alkaid.anime.service.impl;

import com.alkaid.anime.mapper.CoinsMapper;
import com.alkaid.anime.mapper.UserMapper;
import com.alkaid.anime.service.CoinsService;
import com.alkaid.anime.utils.RedisUtils;
import com.alkaid.commons.core.CacheKeyDefinition;
import com.alkaid.commons.entity.Coins;
import com.alkaid.commons.entity.User;
import com.alkaid.commons.ex.UnknownErrorException;
import com.alkaid.commons.utils.SnowflakeIdWorker;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @author Kylin
 */
@Service
public class CoinsServiceImpl implements CoinsService {

    private final CoinsMapper coinsMapper;

    private final UserMapper userMapper;

    private final RedisUtils redisUtils;

    public CoinsServiceImpl(CoinsMapper coinsMapper, UserMapper userMapper, RedisUtils redisUtils) {
        this.coinsMapper = coinsMapper;
        this.userMapper = userMapper;
        this.redisUtils = redisUtils;
    }


    @Override
    public Boolean getCoins(Long userId, Integer animeId) throws UnknownErrorException {
        // 获取key
        String key = CacheKeyDefinition.getCoinsKey(userId, animeId);
        // 从缓存中查询
        Boolean result = (Boolean) redisUtils.getFromRedis(key);
        // 如果缓存中没有则从数据库查询
        if (result == null) {
            Coins coins = getCoinsBySelect(userId, animeId);
            if (coins == null) {
                return false;
            }
            redisUtils.save2Redis(key, true, CacheKeyDefinition.CACHE_TIME_DAY);
        }
        return result;
    }

    @Override
    public void setCoins(Coins select) throws UnknownErrorException {
        Coins coins = getCoinsBySelect(select.getUserId(), select.getAnimeId());
        if (coins != null) {
            throw new UnknownErrorException("你已经投过币了哦(*^▽^*)");
        }
        User selectUser = new User();
        selectUser.setId(select.getUserId());
        User user = userMapper.selectById(selectUser);
        System.out.println(user);
        if (user.getCoins() < 1) {
            throw new UnknownErrorException("你的硬币不足哦ヽ(ー_ー)ノ");
        }
        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 2);
        select.setId(idWorker.nextId());
        select.setCoinsRange(-1);
        Integer result = coinsMapper.insert(select);
        if (result < 1) {
            throw new UnknownErrorException("投币失败");
        }
        selectUser.setCoins(user.getCoins() - 1);
        String key = CacheKeyDefinition.getCoinsKey(select.getUserId(), select.getAnimeId());
        redisUtils.save2Redis(key, true, CacheKeyDefinition.CACHE_TIME_DAY);
    }

    private Coins getCoinsBySelect(Long userId, Integer animeId) {
        HashMap<SFunction<Coins, ?>, Object> map = new HashMap<>();
        map.put(Coins::getUserId, userId);
        map.put(Coins::getAnimeId, animeId);
        return coinsMapper.selectOne(new LambdaQueryWrapper<Coins>().allEq(map));
    }
}
